<?php 
class Home extends FrontEnd_Controller {
    function Home(){
        parent::FrontEnd_Controller();
        $this->_data['erreur'] = "";
        $this->load->model('Home_m');
     }
    function login(){
            $config = array(
                array('field' => 'email',
                    'rules' => 'required|valid_email'
                ),
                array('field' => 'pass',
                    'rules' => 'required|md5'
                )
            );
            $this->form_validation->set_rules($config);
            if($this->input->post('submit')){
            if ($this->form_validation->run() == TRUE) {
                $data = array(
                    'email' => $this->input->post('email'),
                    'pass' => $this->input->post('pass')
                );
                $query = $this->Home_m->check_connexion($data);
                if ($query->num_rows() > 0) {
                   
                    $row = $query->row();
                     $dat=time();
                    if($row->date_debut <= $dat&& $row->date_fin >= $dat ) {
                    $this->session->set_userdata('userid', $row->id);
                    $this->session->set_userdata('username', $row->nom);
                    $this->session->set_userdata('adherent', $row->ad);
                    redirect(site_url('home/adherent'));
                    } else {
                        $this->_data['erreur'] = "Votre forfait n'est plus valable !";
                        $this->_data['page']="";
                    $this->display_view('home/login');
                    }
                }else {
                    $this->_data['erreur'] = "Identifiant ou mot de passe n'existe pas";
                    $this->_data['page']="";
                    $this->display_view('home/login');
                }
            } else{
                $this->_data['erreur'] = "";
                $this->_data['page']="";
                $this->display_view('home/login');}
            }
        else{ 
        $this->_data['erreur'] = "";
        $this->_data['page']="";
        $this->display_view('home/login');}    
    }
    function deconnexion() {
        $array_items = array('username' => '', 'userid' => '');
        $this->session->unset_userdata($array_items);
        redirect(base_url() . 'index.php/home');
    }
 
    function index(){
        $page='banner';
        $this->_data['page']="index";
        $this->_data['query1']= $this->Home_m->get_contenu_by_ID(1);
        $this->_data['query2']= $this->Home_m->get_contenu_by_ID(2);
        $this->_data['query3']= $this->Home_m->get_contenu_by_ID(3);
        $this->_data['query']=$this->Home_m->get_image($page);
        $this->display_view('home/index');
    }
    function presentation(){
        $this->_data['page']="presentation";
        $this->_data['chutelibre']=$this->Home_m->get_image_by_name('chutelibre');
        $this->_data['copainsgoal']=$this->Home_m->get_image_by_name('copainsgoal1');
        $this->_data['raft']=$this->Home_m->get_image_by_name('raft');
        $this->_data['meleeteam']=$this->Home_m->get_image_by_name('meleeteam');
        $this->_data['adherents']=$this->Home_m->get_image_by_name('adherents');
        $this->display_view('home/presentation');
    }
    function p_a_setb(){
        $this->_data['page']="presentation";
        $this->_data['text1']=$this->Home_m->get_contenu_by_name('meleeblue+chutelibre');
        $this->_data['text2']=$this->Home_m->get_contenu_by_name('Photo golfeuse');
        $this->_data['text3']=$this->Home_m->get_contenu_by_name('Photo mains');
        $this->_data['text4']=$this->Home_m->get_contenu_by_name('Photo marins');
        $this->_data['text5']=$this->Home_m->get_contenu_by_name('Photo footsupporter');
        $this->_data['chutelibre']=$this->Home_m->get_image_by_name('chutelibre');
        $this->_data['meleebleue']=$this->Home_m->get_image_by_name('meleebleue');
        $this->_data['golfeuse']=$this->Home_m->get_image_by_name('golfeuse2');
        $this->_data['mains']=$this->Home_m->get_image_by_name('mains');
        $this->_data['marins']=$this->Home_m->get_image_by_name('marins');
        $this->_data['supporter']=$this->Home_m->get_image_by_name('supporter');
        $this->display_view('home/p_a_setb');
    }
    function principe(){
        $this->_data['page']="presentation";
        $this->_data['text1']=$this->Home_m->get_contenu_by_name('Photo copainsgoal');
        $this->_data['text2']=$this->Home_m->get_contenu_by_name('objectif');
         $this->_data['copainsgoal']=$this->Home_m->get_image_by_name('copainsgoal1');
        $this->_data['raft']=$this->Home_m->get_image_by_name('raft');
        $this->_data['relais']=$this->Home_m->get_image_by_name('relais');
        $this->_data['regate']=$this->Home_m->get_image_by_name('regate');
        $this->_data['squash']=$this->Home_m->get_image_by_name('squash1');
        $this->display_view('home/principe');
    }
    function equipe_adherents(){
        $this->_data['page']="presentation";
        $this->_data['text1']=$this->Home_m->get_contenu_by_name('meleeteam');
        $this->_data['text2']=$this->Home_m->get_contenu_by_name('Photo adherents');

        $this->_data['meleeteam']=$this->Home_m->get_image_by_name('meleeteam');
        $this->_data['adherents']=$this->Home_m->get_image_by_name('adherents');
        $this->display_view('home/equipe_adherents');
    }
    
    function fonctionnement(){
        $this->_data['page']="fonctionnement";
        $this->_data['text1']=$this->Home_m->get_contenu_by_name('introduction');
        $this->display_view('home/fonctionnement');
    }
    function pourquoi_adherer(){
         $this->_data['page']="fonctionnement";
        $this->_data['text1']=$this->Home_m->get_contenu_by_name('pourquoi adherer');
        $this->_data['ski']=$this->Home_m->get_image_by_name('ski');
        $this->_data['regate']=$this->Home_m->get_image_by_name('regate');
        $this->_data['surfeur']=$this->Home_m->get_image_by_name('surfeur');
        $this->_data['foot']=$this->Home_m->get_image_by_name('foot');
        $this->display_view('home/pourquoi_adherer');
    }
    function reg_cont(){
        $this->_data['page']="fonctionnement";
        $this->_data['text1']=$this->Home_m->get_contenu_by_name('regles');
        $this->_data['text2']=$this->Home_m->get_contenu_by_name('condition');
        $this->_data['natation']=$this->Home_m->get_image_by_name('natation');
        $this->display_view('home/reg_cont');
    }
    function mention_legale(){
       
        $this->_data['text1']=$this->Home_m->get_contenu_by_name('mention legale');
        $this->_data['page']="";
        $this->display_view('home/mention_legale');
    }
    function contact(){
        $this->_data['page']="";
        $this->form_validation->set_rules('nom', 'Nom', 'required|xss_clean');
        $this->form_validation->set_rules('prenom', 'Prenom', 'required|xss_clean');
        $this->form_validation->set_rules('entreprise', 'Votre entreprise', 'required|xss_clean');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|xss_clean');
        $this->form_validation->set_rules('adresse', 'Adress', 'xss_clean');
        $this->form_validation->set_rules('ville', 'Ville', 'xss_clean');
        $this->form_validation->set_rules('code postale', 'Code postale', 'xss_clean');
        $this->form_validation->set_rules('sujet ', 'Sujet', 'xss_clean');
        $this->form_validation->set_rules('tel', 'Telephone', 'required|xss_clean');
        $this->form_validation->set_rules('message', 'Votre message', 'required|xss_clean');
            
        if($this->input->post('submit')){
            if($this->form_validation->run()){
                    $data= array(
                    'nom' => $this->input->post('nom'),
                    'prenom' => $this->input->post('prenom'),
                    'adresse' => $this->input->post('adresse'),
                    'entreprise' => $this->input->post('entreprise'),                    
                    'ville' => $this->input->post('ville'),
                    'code_postale' => $this->input->post('code postale'),
                    'tel' => $this->input->post('tel'),
                    'email' => $this->input->post('email'),
                    'message' => $this->input->post('message'),
                    'date'=> date('Y-m-d'),
                    'sujet' => $this->input->post('sujet')
                    );
                    $subject= $this->input->post('sujet');
                    $email=$this->input->post('email');
                    $message="Nom : ".$this->input->post('nom');
                    $message.=" ".$this->input->post('prenom');
                    $message.=" | Telephone  : ".$this->input->post('tel');
                    $message.=" | Description : ".$this->input->post('message');       
                    //mail("mrtech123@gmail.com", "$subject", $message, "From: $email" );
                    $this->Home_m->contact($data);
                    $this->_data['nom']="";
                    $this->_data['prenom']="";
                    $this->_data['entreprise']="";
                    $this->_data['adresse']="";
                    $this->_data['ville']="";
                    $this->_data['code_postale']="";
                    $this->_data['tel']="";
                    $this->_data['sujet']="";
                    $this->_data['email']="";
                    $this->_data['message']="";
                    $info="Votre demande a été envoyé!";
                    $retour="Retour à l’accueil.";
                    $this->_data['info']=$info;
                    $this->_data['retour']=$retour;
                    $this->_data['page']="";
                    $this->display_view('home/contact');
                }
                else {
                    $this->_data['nom']=$this->input->post('nom');
                    $this->_data['entreprise']=$this->input->post('entreprise');
                    $this->_data['adresse']=$this->input->post('adresse');
                    $this->_data['ville']=$this->input->post('ville');
                    $this->_data['code_postale']=$this->input->post('code postale');
                    $this->_data['prenom']=$this->input->post('prenom');
                    $this->_data['tel']=$this->input->post('tel');
                    $this->_data['sujet']=$this->input->post('sujet');
                    $this->_data['email']=$this->input->post('email');
                    $this->_data['message']=$this->input->post('message');
                    $this->_data['info']="Vous avez besoin de remplir tous les infornations";
                    $this->_data['retour']="";
                    $this->_data['page']="";
                    $this->display_view('home/contact');
                }
            }
            else{
                $this->_data['nom']="";
                $this->_data['prenom']="";
                $this->_data['entreprise']="";
                $this->_data['adresse']="";
                $this->_data['ville']="";
                $this->_data['code_postale']="";
                $this->_data['tel']="";
                $this->_data['sujet']="";
                $this->_data['email']="";
                $this->_data['message']="";
                $this->_data['info']="";
                $this->_data['retour']="";
                $this->_data['page']="";
                $this->display_view('home/contact');
            }       

        }
    function adherent(){
        is_adherent();
        $this->_data['region'] = $this->Home_m->get_region();
        $this->_data['secteur'] = $this->Home_m->get_secteur();
        $this->_data['sports'] = $this->Home_m->get_sports();
        $this->_data['recherche'] = $this->Home_m->get_recherche();
        $sport="";
        $secteur="";
        $region="";
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 10;
        }

        if ($offset == "") {
            $offset = 0;
        }
        $config['base_url'] = base_url() . 'index.php/home/recherche_adherent/' . $per_page . '/';     $config['per_page'] = '10';
        $config['uri_segment'] = 4;
        $query=$this->Home_m->recherche_adherent($sport,$secteur,$region,$per_page,$offset);
        $query1=$this->Home_m->recherche_adherent1($sport,$secteur,$region);
        $config['total_rows'] =$query1->num_rows();
        $this->pagination->initialize($config);
        //$this->_data['side_bar']="gestion_image";
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['num']=$query1->num_rows();
        $this->_data['query']=$query->result_array();
        $this->_data['display']="none";
        $this->_data['page']="adherence";
        $this->display_view('home/adherent');

    }
    function recherche_adherent(){
        is_adherent();
        $sport=$this->input->post('sport'); 
        $secteur=$this->input->post('secteur');
        $region=$this->input->post('region');
        $this->_data['recherche'] = $this->Home_m->get_recherche();       
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 10;
        }

        if ($offset == "") {
            $offset = 0;
        }
        $config['base_url'] = base_url() . 'index.php/home/recherche_adherent/' . $per_page . '/';     $config['per_page'] = '10';
        $config['uri_segment'] = 4;
        $query=$this->Home_m->recherche_adherent($sport,$secteur,$region,$per_page,$offset);
        $query1=$this->Home_m->recherche_adherent1($sport,$secteur,$region);
        $config['total_rows'] =$query1->num_rows();
        $this->pagination->initialize($config);
        //$this->_data['side_bar']="gestion_image";
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['num']=$query1->num_rows();
        $this->_data['query']=$query->result_array();

                $this->_data['select_region'] = $region;
        $this->_data['select_secteur'] = $secteur;
        $this->_data['select_sport'] = $sport;

        $this->_data['region'] = $this->Home_m->get_region();
        $this->_data['secteur'] = $this->Home_m->get_secteur();
        $this->_data['sports'] = $this->Home_m->get_sports();
        $this->_data['display']="block";
        $this->_data['page']="adherence";
        $this->display_view('home/adherent');
    }
    function detail_adherent($id){
        is_adherent();
        $this->_data['sports'] = $this->Home_m->get_sports();
        $this->_data['recherche'] = $this->Home_m->get_recherche();
        $query=$this->Home_m->get_adherent_by_ID($id);
        $this->_data['query']=$query->row();        
        $this->_data['page']="adherence";
        $this->display_view('home/detail_adherent');
    }

   function pass_oublier(){
        $config = array(
                array('field' => 'email',
                    'rules' => 'required|valid_email'
                ));
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {

            $email=$this->input->post('email');
            $query=$this->Home_m->check_email($email);
            if($query->num_rows()>0) {
                $query=$query->row();
             $id=$query->id;
            $string=random_string('alnum', 16);
            $stringmd5=md5($string);
            $get_message= $this->Home_m->get_mail(5);
            $message=$get_message["contenu"];
            $message=str_replace("%password%", $string, $message);
            $data1 = array( 'pass'=> $stringmd5 );
             $this->Home_m->update_client($data1, $id);
             mail($email, $get_message["titre"], $message, "From: contact@gwebcreation.com" );
              $this->_data['erreur'] = "Mot de passe a été envoyé à votre email !";
                  } else{
                $this->_data['erreur'] = "Email n'existe pas!";
            }

            }
            }
      $this->_data['page']="Mot de passe oublier";
      $this->display_view('home/pass_oublier');
   }

    function recherche_contenu(){
        $keyword=$this->input->post('searchForm');
        $per_page = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 10;
        }

        if ($offset == "") {
            $offset = 0;
        }
        $config['base_url'] = base_url() . 'index.php/home/recherche_contenu/' . $per_page . '/';     $config['per_page'] = '10';
        $config['uri_segment'] = 4;
        $query=$this->Home_m->recherche_contenu($keyword,$per_page,$offset);
        $query1=$this->Home_m->recherche_contenu1($keyword);
        $config['total_rows'] =$query1->num_rows();
        $this->pagination->initialize($config);
        //$this->_data['side_bar']="gestion_image";
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['num']=$query1->num_rows();
        $this->_data['query']=$query->result_array();        
        $this->_data['page']="Recheche";
        $this->display_view('home/recherche_contenu');
    }
   

}

?>
